package com.geekaca.news.controller.admin;

import com.geekaca.news.domain.NewsComment;
import com.geekaca.news.service.CommentService;
import com.geekaca.news.utils.NewsConstants;
import com.geekaca.news.utils.PageResult;
import com.geekaca.news.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @GetMapping("/comments")
    public String comments(HttpServletRequest request) {
        request.setAttribute("path", "comments");
        //指向页面
        return "admin/comment";
    }

    @GetMapping("/comments/list")
    @ResponseBody
    public Result commentList(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {

        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 20;
        }

        //查询记录条数
        Integer allCommentsCount = commentService.getTotalComments();
        List<NewsComment> commentList = commentService.getCommentList(page, limit);

        Result rs = new Result();
        rs.setResultCode(NewsConstants.RESULT_OK);
        PageResult pageResult = new PageResult(commentList, allCommentsCount, limit, page);

        rs.setData(pageResult);
        return rs;
    }


    @PostMapping("/comments/reply")
    @ResponseBody
    public Result commentReply(@RequestParam("commentId") Integer commentId,
                               @RequestParam("replyBody") String replyBody) {
        System.out.println("commentId: " + commentId + " replyBody:" + replyBody);

        int i = commentService.updateComment(commentId, replyBody);
        Result result = new Result();
        if (i>0){
            result.setResultCode(200);
            result.setMessage("回复成功 ");
        }else{
            result.setResultCode(500);
            result.setMessage("操作失败");
        }
        return result;
    }

    @PostMapping("/comments/delete")
    @ResponseBody
    public Result commentDelete(@RequestBody List<Long> ids){
        System.out.println("ids: "+ids);
        Result result = new Result();
        ids.forEach(id->{
            int i = commentService.deleteCommentByid(id);
            if(i>0){
                result.setResultCode(NewsConstants.RESULT_OK);
                result.setMessage("删除成功");
            }else{
                result.setResultCode(NewsConstants.RESULT_FAIL);
                result.setMessage("删除失败");
            }
        });
        return result;
    }
    @PostMapping("/comments/checkDone")
    @ResponseBody
    public Result checkDone(@RequestBody List<Long> ids    ){
        System.out.println("ids: "+ids);
        Result result = new Result();
        ids.forEach(id->{
            int i = commentService.checkDone(id);
            if(i>0){
                result.setResultCode(NewsConstants.RESULT_OK);
                result.setMessage("审核成功");
            }else{
                result.setResultCode(NewsConstants.RESULT_FAIL);
                result.setMessage("审核失败");
            }
        });
        return result;
    }
}
